package com.yinsin.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatcherUtils {

	/**
	 * 正则表达式：验证手机号
	 */
	public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";

	/**
	 * 正则表达式：验证邮箱
	 */
	public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

	/**
	 * 正则表达式：验证汉字
	 */
	public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";

	/**
	 * 正则表达式：验证身份证
	 */
	public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";

	/**
	 * 正则表达式：验证URL
	 */
	public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";

	/**
	 * 正则表达式：验证IP地址
	 */
	public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";

	public static String match(String p, String str) {
		Pattern pattern = Pattern.compile(p);
		Matcher m = pattern.matcher(str);
		if (m.find()) {
			return m.group(1);
		}
		return null;
	}

	public static boolean isMobile(String mobile) {
		return Pattern.matches(REGEX_MOBILE, mobile);
	}

	public static boolean isEmail(String email) {
		return Pattern.matches(REGEX_EMAIL, email);
	}

	public static boolean isChinese(String str) {
		return Pattern.matches(REGEX_CHINESE, str);
	}

	public static boolean isIdcard(String card) {
		return Pattern.matches(REGEX_ID_CARD, card);
	}

	public static boolean isUrl(String url) {
		return Pattern.matches(REGEX_URL, url);
	}

	public static boolean isIp(String ip) {
		return Pattern.matches(REGEX_IP_ADDR, ip);
	}

}
